<!-- 个人中心 -->
<template>
	<view class="personal-wrap">
		<!-- 个人信息卡片 -->
		<userinfo-card :scrollTop="scrollTop" :detail="userHeadData" @onShare="onShare"></userinfo-card>

		<!-- 自定义模块 -->
		<view v-for="(item, index) in userTemplate" :key="item.id">
			<!-- 轮播 -->
			<sh-banner
				v-if="item.type === 'banner' && index !== 0"
				:Px="item.content.x"
				:Py="item.content.y"
				:borderRadius="item.content.radius"
				:height="item.content.height"
				:list="item.content.list"
			></sh-banner>

			<!-- 搜索 -->
			<sh-search v-if="item.type === 'search'"></sh-search>

			<!-- 滑动宫格 -->
			<sh-grid-swiper v-if="item.type === 'menu'" :list="item.content.list" :oneRowNum="item.content.style"></sh-grid-swiper>

			<!-- 推荐商品 -->
			<sh-hot-goods v-if="item.type === 'goods-list' || item.type === 'goods-group'" :detail="item.content"></sh-hot-goods>
			<!-- 广告魔方 -->
			<sh-adv v-if="item.type === 'adv'" :detail="item.content"></sh-adv>
			<!-- 优惠券 -->
			<sh-coupon v-if="item.type === 'coupons'" :detail="item.content"></sh-coupon>
			<!-- 秒杀-->
			<sh-seckill v-if="item.type === 'seckill'" :detail="item.content"></sh-seckill>
			<!-- 拼团 -->
			<sh-groupon v-if="item.type === 'groupon'" :detail="item.content"></sh-groupon>
			<!-- 富文本 -->
			<sh-richtext v-if="item.type === 'rich-text'" :richId="item.content.id"></sh-richtext>
			<!-- 功能列表 -->
			<sh-cell v-if="item.type === 'nav-list'" :list="item.content.list"></sh-cell>
			<!-- 九宫格列表 -->
			<sh-grid v-if="item.type === 'grid-list'" @onShare="onShare" :list="item.content.list"></sh-grid>
			<!-- 功能标题 -->
			<sh-title-card v-if="item.type === 'title-block'" :title="item.content.name" :bgImage="item.content.image" :titleColor="item.content.color"></sh-title-card>
			<!-- 钱包 -->
			<sh-wallet v-if="item.type === 'wallet-card'"></sh-wallet>
			<!-- 代理 -->
			<sh-agent v-if="item.type === 'wallet-card'" :direct="userData.teamcount && userData.teamcount.direct || 0"  :amount="userInfo.count && userInfo.count.amount || '0'" :total="userInfo.count && userInfo.count.total || 0"></sh-agent>
			<!-- 订单卡片 -->
			<sh-order-card v-if="item.type === 'order-card'"></sh-order-card>
			<!-- 直播 -->
			<!-- #ifdef MP-WEIXIN -->
			<sh-live v-if="item.type === 'live' && HAS_LIVE" :detail="item.content"></sh-live>
			<!-- #endif -->
		</view>

		<!-- App下载 -->
		<!-- #ifndef APP-PLUS -->
		<view class="user-list u-flex u-row-between" @click="jump('http://m.gzstkj.top/app/index.html')" style="display: none;">
			<text class="list-name">App下载</text>
			<view class="u-flex">
				<text class="list-val"></text>
				<u-icon class="u-m-l-20" name="arrow-right" size="26" color="#999"></u-icon>
			</view>
		</view>
		<!-- #endif -->

		<!-- #ifdef APP-PLUS -->
		<view class="user-list u-flex u-row-between" @click="checkVersion">
			<text class="list-name">App版本更新</text>
			<view class="u-flex">
				<text class="list-val">当前版本 {{ version }}</text>
				<u-icon class="u-m-l-20" name="arrow-right" size="26" color="#999"></u-icon>
			</view>
		</view>
		<!-- #endif -->

	
		<!-- copyright -->
		<view class="copyright-box u-flex-col u-row-center u-col-center u-p-t-80 u-p-b-50" v-if="shop.copyright">
			<!-- <view class="copyright-text">{{ shop.copyright[0] }}</view> -->
			<!-- <view class="copyright-text" @click="$Router.push({path: '/pages/app/agent/rank'})">{{ shop.copyright[0] }}</view> -->
			<view class="copyright-text">{{ shop.copyright[1] }}</view>
		</view>

		<!-- #ifdef H5 -->
		<view class="tabbar-hack" style="height: 120rpx; width:100%;"></view>
		<!-- #endif -->
		<!-- 关注弹窗 -->
		<shopro-float-btn></shopro-float-btn>
		<!-- 登录提示 -->
		<shopro-auth-modal v-if="authType" @pushMergeConfirmToTop="mergeConfirmDo" ref="authModal"></shopro-auth-modal>
		<!-- 分享组件 -->
		<shopro-share v-model="showShare" :shareDetail="shareInfo" posterType="user"></shopro-share>
		
		<merge-confirm :show.sync="mergeConfirm.show" :oldId.sync="mergeConfirm.oldId" @pushConfirmYesToTop="confirmMergeYes"></merge-confirm>
	</view>
</template>

<script>
import shBanner from './components/sh-banner.vue';
import shGridSwiper from './components/sh-grid-swiper.vue';
import shHotGoods from './components/sh-hot-goods.vue';
import shAdv from './components/sh-adv.vue';
import shCoupon from './components/sh-coupon.vue';
import shSeckill from './components/sh-seckill.vue';
import shGroupon from './components/sh-groupon.vue';
import shRichtext from './components/sh-richtext.vue';
import shCell from './components/sh-cell.vue';
import shGrid from './components/sh-grid.vue';
import shTitleCard from './components/sh-title-card.vue';
import shOrderCard from './components/sh-order-card.vue';
import shWallet from './components/sh-wallet.vue';
import shAgent from './components/sh-agent.vue';
import shSearch from './components/sh-search.vue';
import shCategoryTabs from './components/sh-category-tabs.vue';
import mergeConfirm from './components/merge-confirm.vue'

import userinfoCard from './user/userinfo-card.vue';

import { mapMutations, mapActions, mapState } from 'vuex';

// #ifdef MP-WEIXIN
import { HAS_LIVE } from '@/env';
import shLive from './components/sh-live.vue';
// #endif

import share from '@/shopro/share';

export default {
	components: {
		shBanner,
		shGridSwiper,
		shGroupon,
		shHotGoods,
		shAdv,
		shCoupon,
		shSeckill,
		shRichtext,
		shCell,
		shGrid,
		shTitleCard,
		shWallet,
		shAgent,
		shOrderCard,
		shSearch,
		shCategoryTabs,
		userinfoCard,
		mergeConfirm,

		// #ifdef MP-WEIXIN
		shLive
		// #endif
	},
	data() {
		return {
			// #ifdef MP-WEIXIN
			HAS_LIVE: HAS_LIVE,
			// #endif
			scrollTop: 0,
			showShare: false,
			enable: false, //是否开启吸顶。
			version: '1.0.0',
			mergeConfirm:{
				show: false,
				oldId: 0,
				type: ''
			}
		};
	},
	computed: {
		...mapState({
			shop: ({ shopro }) => shopro.config?.shop, // 版本信息
			userTemplate: ({ shopro }) => shopro.template?.user, //模板数据
			userInfo: ({ user }) => user.userInfo,
			userData: ({ user }) => user.userData,
			isLogin: ({ user }) => user.isLogin,
			authType: ({ user }) => user.authType
		}),
		userHeadData() {
			if (this.userTemplate?.length) {
				return this.userTemplate[0].content;
			}
		}
	},
	// 下拉刷新
	onPullDownRefresh() {
		this.init();
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop;
	},

	onShow() {
		if (this.isLogin) {
			this.init();
			this.getUserData();
		}
		this.enable = true;
		this.$store.commit('CART_NUM');
		// #ifdef APP-PLUS
		this.version = plus.runtime.version;
		// #endif
	},

	methods: {
		...mapActions(['getUserInfo', 'showAuthModal', 'getUserData']),
		onShare() {
			this.shareInfo = share.setShareInfo();
			this.showShare = true;
		},
		// 初始化
		init() {
			this.getUserInfo()
				.then(res => {
					uni.stopPullDownRefresh();
				})
				.catch(e => {
					uni.stopPullDownRefresh();
				});
		},

		jump(url) {
			window.location.href = url;
		},
		
		mergeConfirmDo(data) {
			this.mergeConfirm.show = true;
			this.mergeConfirm.oldId = data.id;
			this.mergeConfirm.type = data.type;
		},
		confirmMergeYes(type) {
			this.mergeConfirm.show = false;
			if (type) this.$refs.authModal[this.mergeConfirm.type]();
		},

		checkVersion() {
			let that = this;
			if (plus.os.name == 'Android') {
				that.$http('common.checkVersion', { appid: plus.runtime.appid, version: plus.runtime.version }).then(res => {
					if (res.code === 1) {
						uni.showModal({
							title: '更新提示',
							content: res.msg,
							success: ret => {
								if (ret.confirm) {
									//plus.runtime.openURL(res.android_url);
									var dtask = plus.downloader.createDownload(res.android_url, {}, function(d, status) {
										// 下载完成
										if (status == 200) {
											plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(error) {
												uni.showToast({
													title: '安装失败',
													duration: 1500
												});
											});
										} else {
											uni.showToast({
												title: '更新失败',
												duration: 1500
											});
										}
									});
									dtask.start();
									var prg = 0;
									var showLoading = plus.nativeUI.showWaiting('正在下载');
									dtask.addEventListener('statechanged', function(task, status) {
										// 给下载任务设置一个监听 并根据状态 做操作
										switch (task.state) {
											case 1:
												showLoading.setTitle('正在下载');
												break;
											case 2:
												showLoading.setTitle('已连接到服务器');
												break;
											case 3:
												prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);
												showLoading.setTitle('版本更新,正在下载' + prg + '% ');
												break;
											case 4:
												plus.nativeUI.closeWaiting();
												//下载完成
												break;
										}
									});
									//plus.runtime.openURL(res.ios_url);
								}
							}
						});
					} else {
						uni.showModal({
							showCancel: false,
							title: '当前已是最新版本'
						});
					}
				});
			}
		}
	}
};
</script>

<style lang="scss">
.contact {
	text-align: center;
	margin-top: 30rpx;
	.tel {
		text-align: center;
		padding: 20rpx;
		color: #999;
		a {
			color: #cc0000;
			text-decoration: none;
		}
	}
}
.copyright-box {
	.copyright-text {
		font-size: 22rpx;
		font-weight: 500;
		color: #c4c4c4;
	}
}
// 个人信息项
.user-list {
	background: #fff;
	height: 100rpx;
	border-bottom: 1rpx solid #f6f6f6;
	padding: 0 30rpx;
	.list-name {
		font-size: 28rpx;
	}
	.avatar {
		width: 67rpx;
		height: 67rpx;
		border-radius: 50%;
	}
	.cuIcon-right {
		margin-left: 25rpx;
	}
	.list-val {
		color: #999;
		font-size: 28rpx;
		text-align: right;
	}
}
</style>
